const App = getApp()
import { HomeModel } from '../../../models/home.js'
const ModelHome = new HomeModel()
import { decodeUnicode } from '../../../utils/index.js'

Page({
  data: {
    discoverList: [],
    pageIndex: 1,
    pageSize: 5,
    loading: false,
    hasNext: true,
    specialId:'',
    defaultImg: '%ASSETS_IMG%/c-face1@2x.png',
    empty:false,
    statusBarHeight: App.globalData.statusBarHeight,
    isTabbar:false,
  },

  //获取发现列表
  getDiscoverList: function () {
    const self = this;
    const params = {
      specialId:self.data.specialId,
      // specialId: '8a6620871af9490f8bd9fb40336ff3d0',
      pageIndex: self.data.pageIndex,
      pageSize:self.data.pageSize
    }

    ModelHome.getDiscoverList(params).then(res => {
      const evaluateInfoVoList = res.data.evaluateInfoVoList;
      if (evaluateInfoVoList.length == 0) {
        self.data.empty = true
      } else {
        self.data.empty = false
      }
      for (var i = 0; i < evaluateInfoVoList.length; i++){
        evaluateInfoVoList[i].evaluateDes = decodeUnicode(evaluateInfoVoList[i].evaluateDes)
      }
      self.setData({
        empty:self.data.empty,
        discoverList: evaluateInfoVoList,
        hasNext: res.data.hasNext,
        loading: false
      })
      wx.stopPullDownRefresh();
    }).catch();

  },

  //缩略图
  preview: function (e) {
    const url = e.currentTarget.dataset.url;
    const urls = e.currentTarget.dataset.urls;
    const urlArr = [];
    for (var i = 0; i < urls.length;i++) {
      urlArr.push(urls[i].imgUrl)
    }
    wx.previewImage({
      current: url, // 当前显示图片的http链接
      urls: urlArr // 需要预览的图片http链接列表
    })
  },

  //头像错误默认处理
  errorImage: function (e) {
    if (e.type == "error") {
      var errorImgIndex = e.target.dataset.index //获取错误图片循环的下标
      var discoverList = this.data.discoverList 　　　　　　　//将图片列表数据绑定到变量
      discoverList[errorImgIndex] = this.data.defaultImg //错误图片替换为默认图片
      this.setData({
        discoverList: discoverList
      })
    }
  },

  onLoad: function (options) {
    this.setData({
      specialId:options.specialId
    })
    this.getDiscoverList()
  },

  loadMore(isPull) {
    //加载更多
    const self = this;
    if (self.data.loading) {
      return
    }
    if (self.data.hasNext) {
      const params = {
        specialId: self.data.specialId,
        pageIndex: self.data.pageIndex,
        pageSize:self.data.pageSize
      }
      // 之后上拉加载的时候显示loading，第一次加载不显示
      if (isPull) {
        self.setData({
          loading: true
        })
      }
      ModelHome.getDiscoverList(params).then(res => {
        const evaluateInfoVoList = res.data.evaluateInfoVoList;
        for (var i = 0; i < evaluateInfoVoList.length; i++){
          evaluateInfoVoList[i].evaluateDes = decodeUnicode(evaluateInfoVoList[i].evaluateDes)
        }
        const tempArray = self.data.discoverList.concat(evaluateInfoVoList)
        self.setData({
          discoverList: tempArray,
          hasNext: res.data.hasNext,
          loading: false
        })
      })
    }
  },

  //上滑到底
  lower: function () {
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },


  onReachBottom() {
    this.data.pageIndex = this.data.pageIndex + 1
    this.loadMore(true)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageIndex:1, 
      pageSize: 5
    })
    this.getDiscoverList();
  },

})
